package model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

/**
 * The persistent class for the schedule database table.
 * 
 */
@Entity
@NamedQuery(name = "Schedule.findIsSchedulePossible", query = "SELECT s FROM Schedule s WHERE  s.train = :tr  AND "
		+ "(s.dateOfDeparture BETWEEN :dateBegin AND :dateEnd) AND"
		+ "(s.dateOfArrival BETWEEN :dateBegin AND :dateEnd)")
public class Schedule implements Serializable {
	private static final long serialVersionUID = 1L;
	
	public static final String FIND_IS_SCHEDULE_POSSIBLE = "Schedule.findIsSchedulePossible";

	@Id
	private int scheduleID = Math.abs(UUID.randomUUID().hashCode());

	@Temporal(TemporalType.TIMESTAMP)
	private Date dateOfArrival;

	@Temporal(TemporalType.TIMESTAMP)
	private Date dateOfDeparture;

	private int version;

	// bi-directional many-to-one association to Train
	@ManyToOne
	@JoinColumn(name = "TrainID")
	private Train train;

	// bi-directional many-to-one association to Station
	@ManyToOne
	@JoinColumn(name = "StationOfArriaval")
	private Station station1;

	// bi-directional many-to-one association to Station
	@ManyToOne
	@JoinColumn(name = "StationOfDeparture")
	private Station station2;

	// bi-directional many-to-one association to Ticket
	@OneToMany(mappedBy = "schedule", cascade = CascadeType.ALL, orphanRemoval = true)
	private List<Ticket> tickets;

	public Schedule() {
	}

	public int getScheduleID() {
		return this.scheduleID;
	}

	public void setScheduleID(int scheduleID) {
		this.scheduleID = scheduleID;
	}

	public Date getDateOfArrival() {
		return this.dateOfArrival;
	}

	public void setDateOfArrival(Date dateOfArrival) {
		this.dateOfArrival = dateOfArrival;
	}

	public Date getDateOfDeparture() {
		return this.dateOfDeparture;
	}

	public void setDateOfDeparture(Date dateOfDeparture) {
		this.dateOfDeparture = dateOfDeparture;
	}

	public Train getTrain() {
		return this.train;
	}

	public void setTrain(Train train) {
		this.train = train;
	}

	public Station getStation1() {
		return this.station1;
	}

	public void setStation1(Station station1) {
		this.station1 = station1;
	}

	public Station getStation2() {
		return this.station2;
	}

	public void setStation2(Station station2) {
		this.station2 = station2;
	}

	public List<Ticket> getTickets() {
		return this.tickets;
	}

	public void setTickets(List<Ticket> tickets) {
		this.tickets = tickets;
	}

	@Version
	public int getVersion() {
		return version;
	}

	@Version
	public void setVersion(int version) {
		this.version = version;
	}
}